To print: Select File and then Print from your browser´s menu. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
An intro to Perl
Matt Kynaston introduces the basics of CGI programming and shows you how to put a chat room on your site
If you've been
building Web pages for a while, sooner or later you'll come across
a site with features you'd dearly love to include in your own. It
could be a discussion board, a site-wide search, pages that let users
enter personal details that the site stores. No amount of tweaking
in your favourite HTML editor can achieve the same effects.
The magic that drives most of these sites are CGI programs. Because
CGI programs run on the server, instead of the users browser, they
can do almost anything.They're normally found in a directory called
the 'cgi-bin', and their power lies in getting information from the
user and doing something with it. Over the next few issues, we're going to open the lid of the cgi-bin,
and show you how to make your site respond to users like never before.
CGI programming sounds intimidating, but all that bin normally contains
are scripts. You should be familiar with HTML before you start, but
won't need anything else. Perl before swine The vast majority of CGI programs are written in a language called
Perl. It started life as a tool to help Unix system administrators
carry out routine, boring tasks. But with the advent of the Web, people
realised it could be used to create interactive online applications.
The back end of the search engine Yahoo was written entirely in Perl
- so were tons of other sites.
Unlike languages like C, Perl scripts aren't compiled into some unintelligible
binary program. Instead you just upload a text file containing the
script, and the Perl interpreter on your host's server takes care
of running it. Ever wondered what that question
mark and other weird stuff is doing in the URL when
you're using Yahoo? That's CGI in action This is important. For one thing, it makes Perl scripts easy to
modify and pass around to your friends, which in turn means there's
tons of free programs out there that, with a little modification,
will do just about anything you could wish for. It also means they
can run on any operating system, so long as it has a Perl interpreter
installed. And the Perl interpreters are free too. We like free.
We're going to show you how to use Perl scripts on your own site
and give you just enough knowledge of the language so you can customise
them if you need to. But this isn't going to be a tutorial on programming
in Perl. If you like what you see and want to start bashing out code
yourself, there's some excellent books that'll help (see the Resources
box) and a whole online community of 'Perl Mongers' waiting to welcome
you into their (sometimes strange) world. There's another reason for only scratching at Perl. Although it's
powerful, well respected, and an excellent thing to have a nodding
acquaintance with, it wasn't originally built for Web design. In recent
years, a whole bunch of other languages have been created just to
handle the task of interactive Web design, including Microsoft's Active
Server Pages (ASP), Java's JSP and a completely Open Source language
called PHP. They're much easier than Perl to learn and use, so we'll
devote the third and fourth part of this series to them. Serving yourself Enough background. Onto some Perl hacking. All you really need to get your scripts working is good old Notepad (or another text editor) and some Web space with a CGI bin. You've got almost certainly Notepad already, and you'll find a list of completely free hosts in the Resources section. But it's useful to try out your Perl scripts on your own computer before you upload them. It'll save you the connection charges while you figure things out, and makes ironing out bugs much easier. For this you'll need a Web server and a Perl interpreter.
Running a Web server sounds like a big deal, and possibly expensive. It isn't. Microsoft used to make a program called Personal Web Server (PWS) that ran on Windows 95 and 98, but unfortunately it no longer supported in Windows Millennium and is tricky to set up to run Perl. But there are other, better servers around. We've settled on Xitami, because it works straight out the box, is easy to use and (is this beginning to sound familiar?) completely free. We've bundled Xitami together with ActivePerl - the version of Perl that runs under Windows - into a single set up program on the CD. Fire it up before going further. If you're already using PWS, the installer will give you the choice of using it instead of Xitami. Please read the Using Personal Web Server box before deciding which to use. Testing Perl Once the setup is finished and your computer has restarted, it's time to try out your first script. When you're running a Web server on your own computer, your machine becomes the 'localhost'. To view Web pages from the server, open your browser and type http://127.0.0.1 into the address bar. This number is a special IP address that always points to the local server. Most computers will also understand the easier to remember localhost address if you type it into your browser.
Try it. You'll get a screen saying 'Welcome to Xitami' with lots of information about the program and links to it's documentation. Now make sure Perl is working properly. Type http://127.0.0.1/cgi-bin/test-perl.pl into the address bar. If all's well, you'll get a message saying 'Hello from Perl' and giving you some information about the server environment. If you run into errors at this point, check Xitami is actually running. If it still doesn't work, the chances are the set up didn't go smoothly. Make sure you're not running another Web server. If not, use Add/Remove Programs in your Control Panel to uninstall Xitami and ActivePerl and run the installer again, making sure you restart your computer when prompted. Getting chatty Now it's time to get Perl doing something useful. We're going to add a simple chat room to our Web site, using a free program called EveryChat. If you've got last month's CD, you'll find it there, or you can download it from www.everysoft.com/everychat. Copy the Zip file to your hard drive and extract the files using WinZip or a similar zip program. Now take a look in the folder you installed Xitami to - it'll normally be C:\Xitami. You'll notice a bunch of folders inside it. The two important ones are the cgi-bin folder and the webpages folder. Copy the evercht.cgi file from zip to your cgi-bin, then create a new folder inside webpages. Call it chat, and make another folder inside it called messages. Copy chatform.html, chatframes.html and chattop.html to the chat folder, then copy messages.html to the messages subfolder. Open chatforms.html in Notepad and find the '<FORM ACTION=..' line. Change the address to '/cgi-bin/everycht.cgi'. Do the same to chatframes.html. When you give a URL starting with a forward slash, the Web server starts at the 'top' of your Web site (the root), and works down from there. In Xitami, this root is the webpages directory. But the cgi-bin is a bit special. Even though it's above the root on your hard drive, the server creates a 'virtual' directory inside the root. This is done for security reasons. Now open everycht.cgi with Notepad. Be careful about using other text editors with Perl scripts - some, such as Word, leave garbage at the ends of the lines that will stop them being read correctly. Notepad might not be pretty, but it doesn't mess with things. Scroll down until you get to the section marked
'CONFIGURATION'. You'll see an indented line starting '$filepath='.
Replace the text between the quotes with full path to your new messages
directory from the root of your drive - it should be something like:
'/Xitami/webpages/chat/messages/'. Notice we've used forward slashes
all the way through, and added one to the end.
Getting it to work Now point your browser at http://127.0.0.1/chat/chatframes.html. Give yourself a name and click Submit Name button. Once the centre screen refreshes, you'll see your name enter the chat room, and you're away. The script doesn't produce the prettiest chat room going, but there's lots of ways you can customise it before letting it lose on the world. Tale a look through the Readme.txt file that came with EveryChat for some ideas. We're going plough on and upload it, however. See Uploading your Chat Room to learn how it's done. Once you've got the basics down, check out the other sections in this article: Bughunting - for when things go wrong, Smartass - for the lowdown on CGI jargon and Resources - for links to great Perl sites. Next month, we'll go into more detail about how Perl scripts are put together so you can start getting your hands dirty in the code. Until then, happy Perl Mongering. Uploading your CGI
Debugging Perl Broken your script? Try some of these tricks to find out what's wrong
Personal Web Server and Perl Personal Web Server comes with Microsoft's Frontpage, and if you're using Windows 98, you can install it straight from your Windows CD. But it's not compatible with Windows Me, and is requires some tweaking before it will happily run Perl scripts. We don't recommend using it for these tutorials, but running two Web servers on one machine can cause problems. If you're hooked on Frontpage and don't want to uninstall PWS, our setup program will give you the choice of letting it make some changes to your registry that will enable Perl to work on PWS. But, unlike Xitami, PWS won't automatically recognise Perl scripts for what they are. Instead, it relies on the file extension of the script. These must be either '.pl' or '.cgi'. Many of the scripts you download won't use these extensions, so you'll have to change not only the file name, but any references to the files in your HTML and even within the Perl script itself. Also be sure to consult both ActivePerl's documentation and PWS's online help for instructions on setting up your CGI bin. Smartass There's nothing like a bunch of jargon to make you feel dumb. Here's what it all means... CGI - Common Gateway Interface. The standard way for a Web server to send and receive data to and from the user cgi-bin - the most common name for the directory on your Web server where CGI scripts are uploaded to. chmod - a Unix program that changes the permissions on files. See 'file permissions'. file permissions - Unix files and directories are given permissions to say who can read and write to them, as well as if they can be executed. Perl scripts and cgi-bin directories should have their permissions set to 755, which indicates anyone can read and execute them, but only their owner can write to them. Host - a company that provides space on their Web server. If you're using CGI and Perl, you must make sure your host is set up to let you run your scripts on their server. See the Resources section Open Source - a licensing model that states that the source code of the software must be freely available, enabling anyone to modify it and contribute to the software's development. See www.opensource.org Perl - Practical Extraction and Reporting Language (or Pathologically Eclectic Rubbish Lister). A scripting language created by Larry Wall to handle Unix system administration tasks. Rapidly became a popular CGI programming language, and has been referred to as 'the glue of the Internet' Web server - a computer that sends Web
pages to a user's browser. There are many different Web server programs
available, including IIS for Windows 2000, Apache and Xitami Perl resources We've only scraped the surface of the wild world of Perl. Here's where to find out more... Perl www.activestate.com www.perl.com www.findtutorials.com www.netmag.co.uk/forums/forum_web_builder.htm Free hosting with CGI www.uklinux.net Other free hosts with CGI - some may place banner
ad on your site. Free scripts www.perlarchive.com www.cgi-resources.com www.bignosebird.com www.scriptsearch.com Books
Learning Perl, 2nd Edition, Schwartz &
Christiansen, O'Reilly, 1997. Learning Perl on Win32 Systems, Swartz,
Olson & Christiansen, O'Reilly, 1997. Perl and CGI for the World Wide Web, Elizabeth
Castro, Addison-Wesley 1998.
This article was first published in .net magazine, issue 81. You can subscribe to .net here or order back issues from backissues@futurenet.co.uk.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
|